<!--
    action控制中心 用于控制actio进行集中进行action跨模块调用
    action容器，用于放置action
    负责action的分发与中转，作为action与owner的桥梁，为了增强兼容性并未采用store


    开发中
    //开发记录
    TODO
    ownerDispatch
    ownerState
    ownerCommit
    ownerGetters

    内部概念
    triggerData 触发数据，用于触发actionTrigger或者actionCaller的数据
    actionData action内部数据，主要用于提交或者从外部获取（如form中的数据为actionData，使下级子表的action获取该数据）

    model action的model
    modelParam model的参数属性，用于做各种判断，默认返回{}以减少空值的判断

    actionTrigger 当前action触发
        -> 传递参数对象
        {
            id,
            data,
            index,
            option //附加数据
        }
    actionCall 跨级调用（parent:true） 或者平级调用
    {
        code,
        parent,
        data:triggerData
    }

    ###标准化action声明周期 ？通过绑定event实现
    param=>

    当同时定义个action button与action modal（主体）,通过code生成storeCode,是的button可以绑定主体的store


    action mirror
    向上找到owner，绑定owner其下属的actionCollcetion

-->

<template>
    <div class="action-collection">
      <template  v-for="action in actionList">
        <ActionTemplate v-if="!action.rewrite" :key="action.code" :code="action.code">
            <component :is="actionComponent(action)"></component>
        </ActionTemplate>
        <component v-else :is="actionComponent(action)" :key="action.code"></component>
      </template>
    </div>
</template>

<script>
import actionLib from "action/lib";
import actionBase from "action/lib/actionBase";
import ActionTemplate from "action/part/actionTemplate/index";
import $ from "@/common";
export default {
  name: "actionCollection",
  props: {
    model: {},
    selectStatus: {
      default: false
    }
  },
  data() {
    return {
      actionCollection: true,
      defaultAction: {
        add: true,
        delete: true,
        view: true,
        edit: true,
        search: true
      }
    };
  },
  computed: {
    //可用action列表，完全排除被屏蔽和开发状态的action
    actionList() {
      var actionModel = this.$owner.actionModel;
      if (!actionModel) {
        if (this.$owner.nodeModel) {
          actionModel = this.$owner.nodeModel.actions;
        } else if (this.$owner.model) {
          actionModel = this.$owner.model.actions;
        }
      }
      return (
        actionModel &&
        actionModel.filter(action => {
          return action.locate !== "none" && (!action.dev || $.dev);
        })
      );
    },
    actionMap() {
      return $.propMap(this.actionList, "code");
    }
  },
  methods: {
    actionComponent(actionType) {
      if (actionLib[actionType]) {
        return "action_" + actionType;
      } else {
        return "actionBase";
      }
    },
    actionHidden(action) {
      return action.locate !== "hidden";
    },
    actionTrigger({ code, data, row, index, cb, option }) {
      if (this.$refs[code] && this.$refs[code][0]) {
        return this.$refs[code][0].actionTrigger(data, row, index, cb, option);
      }
    }
  },
  created() {
    var owner = this;
    //向前上寻找所有者节点（一般为继承了action owner的组件）
    while (owner.$parent) {
      owner = owner.$parent;
      //actionOwner中设置了actionOwner用于此处的判断
      if (owner.actionOwner) break;
    }
    this.$owner = owner;
    /**
         * 为所属组件记录其actionCollction方便后续操作，该设置应移动到node节点的action混合组件，通过向下循环子组件定位actionCollction
         */
    //this.$parent.$actionCollection = this;
    //this.$upperAction owner所属的action
    //this.$upperOwner  upperAction所属的owner 可根据此属性进行上级action调用
    //this.setUpperOwner(this.$upperOwner.$storePath)
    if (this.model && this.model.length) {
      this.model.forEach(action => {
        if (!action.type && this.defaultAction[action.code]) {
          action.type = action.code;
        }
        let type = action.type;

        if (actionLib[type]) {
          let typeCode = type;

          if (actionLib[type].base) {
            typeCode = actionLib[type].base;
          }
          this.$options.components["action_" + type] = resolve =>
            require
              .ensure([], require => {
                resolve(require("action/type/" + typeCode + "/index"));
              })
              .catch(() => {
                //实用统计action加载完成情况
              });
        }
      });
    }
  },
  mounted() {},
  components: {
    actionBase,
    ActionTemplate
  }
};
</script>

<style lang="less">
.action-collection {
  clear: both;
  padding: 0 0 10px;
}
</style>
